package com.example.testrxjava.activity.systemactivity;

import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.example.testrxjava.R;
import com.example.testrxjava.fragment.article.ArticleListFragment;
import com.example.testrxjava.adapter.TabAdapter;
import com.example.testrxjava.base.BaseActivity;
import com.example.testrxjava.fragment.system.KnowLedgeBean;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class SystemTreeActivity extends BaseActivity {
    @BindView(R.id.system_ActionBar)
    AppBarLayout system_ActionBar;
    @BindView(R.id.commonToolbarTv)
    TextView commonToolbarTv;
    @BindView(R.id.secondSystemTabs)
    SlidingTabLayout secondSystemTabs;
    @BindView(R.id.secondSystemViewPager)
    ViewPager secondSystemViewPager;
    @BindView(R.id.commonToolbar)
    Toolbar mCommonToolbar;
    KnowLedgeBean.DataBean bean;
    private String title;
    private List<Fragment> mFragmentList;
    private List<String > mSecondSystemNameList;
    private TabAdapter mTabAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_system_tree;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void init(Bundle savedInstanceState) {

        getBundle();
        setSupportActionBar(mCommonToolbar);
        if(bean==null){
            return;
        }
        title=bean.getName();
        commonToolbarTv.setText(title);
        ActionBar actionBar=getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);//不使用原始的toolbar的title
        }
        mCommonToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        addFragment();


    }


    private void addFragment(){
        mFragmentList = new ArrayList<>();
        mSecondSystemNameList=new ArrayList<String>();
        for (int i = 0; i < bean.getChildren().size(); i++) {
            mFragmentList.add(ArticleListFragment.getInstance(bean.getChildren().get(i).getId()));
            mSecondSystemNameList.add(bean.getChildren().get(i).getName());
        }
        mTabAdapter = new TabAdapter(getSupportFragmentManager(), mFragmentList, mSecondSystemNameList);
        secondSystemViewPager.setAdapter(mTabAdapter);
        secondSystemTabs.setViewPager(secondSystemViewPager);
    }

    @Override
    public void initLinstener() {

    }

    private void getBundle(){
        Bundle bundle=getIntent().getExtras();
        bean= (KnowLedgeBean.DataBean) bundle.getSerializable("bean");

    }

}
